<template>
  <div class="article-form">
    <el-form :model="article" label-width="150px">
      <el-form-item label="文章标题">
        <el-input v-model="article.title" style="width: 82%" />
      </el-form-item>
      
      <el-form-item label="文章内容">
        <div style="border: 1px solid #ccc; margin-top: 20px">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editorRef"
            :defaultConfig="toolbarConfig"
          />
          <Editor
            style="height: 650px; overflow-y: hidden;border-"
            v-model="article.content"
            :defaultConfig="editorConfig"
            @onCreated="handleCreated"
          />
        </div>
      </el-form-item>
      <el-form-item label="文章作者">
        <el-input v-model="article.author" style="width: 180px" disabled />
      </el-form-item>
      <el-form-item label="是否发布">
        <el-switch
          v-model="article.status"
          style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
          active-text="是"
          inactive-text="否"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="submit" type="primary">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
  
</template>

<script>
import { get, put, post } from '@/utils/request' 
import { onBeforeUnmount, shallowRef, reactive } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

import '@wangeditor/editor/dist/css/style.css'
import { createEditor, createToolbar, IEditorConfig, IDomEditor } from '@wangeditor/editor'

export default {
  name:'article-form',
  components: { Editor, Toolbar },
  setup() {
    const article = reactive({})
    const route = useRoute()
    const router = useRouter()
    const articleId = route.query.id
    // 如果存在articleId则是修改文章
    if (articleId) {
      get('/getArticle/' + articleId).then(res => {
        Object.assign(article, res.data) // reactive类型需要设置属性不能直接赋值
      })
    } else {
      const userName = localStorage.getItem('login_user')
      article.author = userName
    }
    // 编辑器实例
    const editorRef = shallowRef()
    const toolbarConfig = {
      excludeKeys: ['uploadImage','uploadVideo']
    }
    const editorConfig = {
      focus: false,
      MENU_CONF: {},
    }
    // 组件卸载时销毁编辑器
    onBeforeUnmount(() => {
        const editor = editorRef.value
        if (editor == null) return
        editor.destroy()
    })

    // 编辑器组件初始化
    const handleCreated = (editor) => {
      editorRef.value = editor // 记录 editor 实例，重要！
    }

    const submit = () => {
      if (articleId) {
        // 修改
        put('/updateArticle', article).then((res) => {
          if (res.data === 1) {
            ElMessage.success('修改文章成功')
            router.back()
          }
        })
      } else {
        // 新增
        post('/addArticle', article).then((res) => {
          if (res.data === 1) {
            ElMessage.success('添加文章成功')
            router.push('/articleList')
          }
        })
      }
    }
    return {
      article,
      editorRef,
      toolbarConfig,
      editorConfig,
      handleCreated,
      submit
    }
  }
}
</script>

<style lang="scss" scoped>
.article-form {
  margin-top: 20px;
}
</style>
